package cc.autoapi.pucong.auto.flow.core.root.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;

/**
 * spring 上下文获取bean
 * @author compuetr
 */
@Component
@Slf4j
public class ApplicationContextUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContextUtil.applicationContext = applicationContext;
    }

    public ApplicationContext getApplicationContext() {
        return ApplicationContextUtil.applicationContext;
    }

    public static Object getBean(String beanName) throws BeansException {
        return ApplicationContextUtil.applicationContext.getBean(beanName);
    }

    public static Object getBean(Class beanName) throws BeansException {
        return ApplicationContextUtil.applicationContext.getBean(beanName);
    }

    public static <T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException {
        return ApplicationContextUtil.applicationContext.getBeansOfType(type);
    }

    /**
     * 获取本机ip
     * @return
     */
    public static String getResponseHost() {
        String ip = "";
        try {
            InetAddress addr = InetAddress.getLocalHost();
            ip = addr.getHostAddress();
        } catch (UnknownHostException unknownHostException) {
            log.error("获取本机ip失败");
        }
        return ip;
    }

    /**
     * 判断是否是开发环境
     * @return
     */
    public static Boolean isDev() {
        return "dev".equals(applicationContext.getEnvironment().getActiveProfiles()[0]);
    }

    public static String getSystemBasePath(){
        String usrdir = System.getProperty("user.dir");
        String basePath = usrdir + File.separator + "data";
        File file = new File(basePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        return basePath;
    }

    public static String getUploadPath(){
        String usrdir = System.getProperty("user.dir");
        String basePath = usrdir + File.separator + "data" + File.separator + "dataFiles";

        File file = new File(basePath);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        if (!file.exists()) {
            file.mkdirs();
        }
        return basePath;
    }

    public static String getJarExtPath(){
        String usrdir = System.getProperty("user.dir");
        String basePath = usrdir + File.separator + "data" + File.separator + "jarExt";

        File file = new File(basePath);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        if (!file.exists()) {
            file.mkdirs();
        }
        return basePath;
    }

    public static String getTmpPath(){
        String usrdir = System.getProperty("user.dir");
        String basePath = usrdir + File.separator + "data" + File.separator + "tmp";

        File file = new File(basePath);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        if (!file.exists()) {
            file.mkdirs();
        }
        return basePath;
    }
}
